package pos.datos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import pos.dominio.*;

public class JDBCUsuariosDAO implements IUsuariosDAO {
	
	
	
	public List selectAllUsuarios() {
		Connection conn = ConnectionManager.getInstance().checkOut();
        PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;
        
        try {

            String sql = "SELECT * FROM Usuario";
            stmt = conn.prepareStatement(sql); 
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            
                    Usuarios temp = new Usuarios();
                    temp.setIDUsuario(result.getString("IDUsuario"));
                    temp.setTipo(result.getString("tipo"));
                    temp.setNombre(result.getString("nombre"));
                    temp.setApellidos(result.getString("apellidos"));
                    temp.setCorrienteDePago(result.getBoolean("corriente_pago"));
                    temp.setTelefono(result.getInt("telefono"));
                    temp.setClave(result.getString("clave"));
                    
                    
                    searchResults.add(temp);
            }
            
            
            
            
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}
	
	public	void añadirUsuario(Usuarios p){
		Connection conn = ConnectionManager.getInstance().checkOut();
		String sql = "INSERT INTO Usuario ( IDUsuario,nombre,apellidos,tipo,telefono,clave,corriente_pago) VALUES (?, ?, ?, ?, ?, ?, ?) ";
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, p.getIDUsuario() );
			stmt.setString(2, p.getNombre());
			stmt.setString(3, p.getApellidos());
			stmt.setString(4, p.getTipo());
			stmt.setInt(5, p.getTelefono());
			stmt.setString(6, p.getClave());
			stmt.setBoolean(7, p.isCorrienteDePago());
			stmt.executeUpdate();

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			ConnectionManager.getInstance().checkIn(conn);
			try {
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		
	}
	
	public void borrarUsuario(String IDUsuario){
		Connection conn = ConnectionManager.getInstance().checkOut();
		String sql = "DELETE FROM Usuario WHERE (IDUsuario = ?) ";
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, IDUsuario);
			stmt.executeUpdate();
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			ConnectionManager.getInstance().checkIn(conn);
			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}
		}
	
		
	}
	
}
